/*
 * author  尘心
 * email   feng_zhenjie@126.com
 * links   http://git.oschina.net/chenxin126/thin
 * version 1.0.2
 * */

! function() {
	'use strict';

	/* -----------------------------
	 * 获取配置文件别名
	 * --------------------------- */
	var scripts = document.getElementsByTagName('script'),
		elem = scripts[scripts.length - 1],
		loader = elem.getAttribute('thin-loader'),
		entran = elem.getAttribute('thin-main'),
		cache = (elem.getAttribute('thin-cache') || 'true') === 'true' ? true : false;

	/* -----------------------------
	 * 获取data-*
	 * --------------------------- */
	var attr_opt = {};
	for(var i in elem.attributes) {
		if(/^data-/.test(elem.attributes[i].name)) {
			attr_opt[elem.attributes[i].name] = elem.attributes[i].value;
		}
	}

	/* -----------------------------
	 * 定义内部变量
	 * moduleMap存储已加载的模块
	 * pathMap存储加载模块路径
	 * length存储当前加载进度
	 * callback存储thin.use
	 * --------------------------- */
	var moduleMap = [],
		pathMap = [],
		length = 0,
		callback;

	/* -----------------------------
	 * 框架别名
	 * --------------------------- */
	var thin = {
		data: attr_opt,

		path: function(pathArrs) {
			pathArrs.length >= 1 ? pathMap = pathArrs : null;
			return this;
		},

		define: function() {
			var that = this;
			if(arguments.length >= 1) {
				var module = arguments[0] instanceof Array ? arguments[0] : [];
				var exports = typeof arguments[1] == 'function' ? arguments[1] : arguments[0];
				if(module.length >= 1) {
					that.require(module);
				}
				exports(function(name, fn) {
					moduleMap[name] = fn;
				});
			}
		},

		use: function() {
			var that = this;
			if(arguments.length >= 1) {
				var module = arguments[0] instanceof Array ? arguments[0] : [];
				callback = typeof arguments[1] == 'function' ? arguments[1] : arguments[0];
				if(module.length >= 1) {
					that.require(module);
				}
			}
		},

		require: function() {
			var module = arguments[0];
			var head = document.getElementsByTagName('head')[0];
			if(module instanceof Array) {
				length += module.length;
				for(var i in module) {
					if(module[i] != moduleMap[i]) {
						createScript(((pathMap[i] || '') + module[i]));
					}
				}
			} else if(typeof module == 'string') {
				length += 1;
				createScript(module, true);
			}

			function createScript(path, jude) {
				if(!/\.js$/.test(path)) path = path + '.js';
				if(cache !== true) path = path + '?_t=' + (new Date()).getTime();
				var node = document.createElement('script');
				node.type = 'text/javascript';
				node.async = 'true';
				node.src = path;
				node.onload = function() {
					head.removeChild(node);
					if(--length == 0) {
						complete(jude);
					}
				};
				head.appendChild(node);
			}
		},

		imports: function() {
			var resource = arguments[0] || null,
				cb = typeof arguments[1] == 'function' ? arguments[1] : null;
			if(!resource) return;
			if(cache !== true) {
				var pr = /\?/.test(resource) ? '&' : '?';
				resource = resource + pr + '_t=' + new Date().getTime();
			}
			var head = document.getElementsByTagName('head')[0];
			if(/\.js/gi.test(resource)) {
				length += 1;
				var node = document.createElement('script');
				node.type = 'text/javascript';
				node.src = resource;
				node.onload = function() {
					head.removeChild(node);
					cb && cb();
					if(--length == 0) {
						complete(true);
					}
				};
				head.appendChild(node);
			} else if(/\.css/gi.test(resource)) {
				var node = document.createElement("link");
				node.type = "text/css";
				node.rel = "stylesheet";
				node.href = resource;
				head.appendChild(node);
			}
		}
	};

	/* -----------------------------
	 * 将AMD的define模块定义到thin
	 * 并回调use函数
	 * --------------------------- */
	function complete(jude) {
		for(var i in moduleMap) {
			thin[i] = moduleMap[i];
		}
		callback && (callback(), callback = null);
	}

	/* -----------------------------
	 * 引入加载器
	 * 引入主文件
	 * --------------------------- */
	(loader && loader != '') && thin.require(loader);
	(entran && entran != '') && thin.require(entran);

	/* -----------------------------
	 * 配置全局变量
	 * --------------------------- */
	window.thin = thin;
}();